package com.myopen.controller;

import java.util.HashMap;
import java.util.Map;

import com.jfinal.aop.Clear;
import com.jfinal.core.ActionKey;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.jfinal.weixin.sdk.api.JsTicket;
import com.jfinal.weixin.sdk.api.JsTicketApi;
import com.jfinal.weixin.sdk.api.SnsAccessToken;
import com.jfinal.weixin.sdk.api.SnsAccessTokenApi;
import com.jfinal.weixin.sdk.kit.PaymentKit;
import com.myopen.base.BaseController;
import com.myopen.base.BaseVo;
import com.myopen.config.ConstConfig;
import com.myopen.kit.UUIDKit;

public class WeixinController extends BaseController{

	@ActionKey("/weixin")
	public void index() {
		//微信首页
	}
	
	@ActionKey("/weixin/auth")
	public void auth() {
		String state=getPara("state");
		String redirectUri= "http://"+PropKit.get("app.host")+"/weixin/redirect";
		
		System.out.println(redirectUri);
		
		String authorizeURL = SnsAccessTokenApi.getAuthorizeURL(PropKit.get("weixin.appid"), redirectUri, state,true);
		
		System.out.println(authorizeURL);
		
		redirect(authorizeURL);
	}
	
	@ActionKey("/weixin/redirect")
	public void skip() {
		
		String code=getPara("code");
		//String state=getPara("state");//暂时用不上
		
		//获取用户openid
		SnsAccessToken snsAccessToken= SnsAccessTokenApi.getSnsAccessToken(PropKit.get("weixin.appid"),PropKit.get("weixin.appSecret"), code);
		String openId=snsAccessToken.getOpenid();
		
		setSessionAttr(ConstConfig.WEIXIN_SESSION_OPEN_ID, openId);
		
		String GOTO_URL_SESSION_ATTRIBUTE=getSessionAttr(ConstConfig.GOTO_URL_SESSION_ATTRIBUTE);
		System.out.println("openId:"+openId);
		if(!StrKit.isBlank(GOTO_URL_SESSION_ATTRIBUTE)) {
			System.out.println(openId+"跳转GOTO_URL_SESSION_ATTRIBUTE:"+GOTO_URL_SESSION_ATTRIBUTE);
			redirect(GOTO_URL_SESSION_ATTRIBUTE);
		}else {
			//如果没有的话
			redirect("/weixin");
		}
		
		//RedPackApi.sendRedPack
	}

	@Clear
	@ActionKey("/getJSSDKPARAM")
	public void getJSSDKPARAM(){
		
		String url = getPara("url");

		String appId=PropKit.get("weixin.appid");
		ApiConfigKit.setThreadLocalAppId(appId);
		
		JsTicket jsTicket=null;
        if(jsTicket==null){
            jsTicket=JsTicketApi.getTicket(JsTicketApi.JsApiType.jsapi);
        }
        if(!jsTicket.isAvailable()){
            jsTicket=JsTicketApi.getTicket(JsTicketApi.JsApiType.jsapi);
        }

		Record record=new Record();
		long timestamp=System.currentTimeMillis()/1000;
		String nonceStr=UUIDKit.getUUID();

        Map<String,String> params=new HashMap<>();
        params.put("jsapi_ticket",jsTicket.getTicket());
        params.put("noncestr",nonceStr); //注意此处noncestr，S是小写
        params.put("timestamp",timestamp+"");
        params.put("url",url);
		
        String signatureStr= PaymentKit.packageSign(params,false);
        String signature= HashKit.sha1(signatureStr);

        record.set("appId",appId);
        record.set("timestamp",timestamp);
        record.set("nonceStr",nonceStr);//注意此处noncestr，S是大写，前台的JS参数是大写
        record.set("signature",signature);

		BaseVo baseVo=new BaseVo();

		baseVo.setCode("0");
		baseVo.setData(record);

        renderJson(baseVo);

	}

}
